package com.sdp.wz.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.BusinessContext;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wz.db.wz_now_num.entity.WzNowNum;
import com.sdp.wz.db.wz_now_num.service.IWzNowNumService;
import com.sdp.wz.db.wz_now_num.service.impl.WzNowNumServiceImpl;
import com.sdp.wz.handleImpl.WzNowNumHandleImpl;
import com.sdp.wz.view.WzNowNumView;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-07-29
 */
@RestController
@RequestMapping("/WzNowNumController")
public class WzNowNumController {

	@Autowired
	private Query query;
	
	@Autowired
	private WzNowNumServiceImpl wzNowNumServiceImpl;
	
	@Autowired
	private IWzNowNumService iWzNowNumService;
	
	@Autowired
	private WzNowNumHandleImpl wzNowNumHandleImpl;
	
	
	@PostMapping(value = "/viewPage")
	public Result<?> viewPage(@RequestBody DataCenter dataCenter) {
		QueryWrapper<WzNowNumView> queryWrapper = new QueryWrapper<>();
		return Result.ok(query.viewPage(wzNowNumServiceImpl, WzNowNumView.class, dataCenter, queryWrapper));
	}
	
	@PostMapping(value = "/alertNumber")
	public Result<?> alertNumber(@RequestBody DataCenter dataCenter) {
		QueryWrapper<WzNowNumView> queryWrapper = new QueryWrapper<>();
		queryWrapper.apply("b.now_num<=b.alert_number");
		return Result.ok(query.viewPage(wzNowNumServiceImpl, WzNowNumView.class, dataCenter, queryWrapper));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WzNowNum wzNowNum) {
		if(wzNowNum==null) {
			return Result.error("商品当前库存信息为空");
		}

		if(StringUtils.isEmpty(wzNowNum.getKeyId())) {
			iWzNowNumService.save(wzNowNum);
		}else {
			iWzNowNumService.updateById(wzNowNum);

		}
		return Result.ok(wzNowNum);
	}
	
	@PostMapping(value = "/init")
	public Result<?> init() {
		BusinessContext businessContext = new BusinessContext();
		businessContext = wzNowNumHandleImpl.handle(businessContext);
		if(businessContext.isSuccess()) {
			return Result.ok();
		}else {
			return Result.error(businessContext.getMessage());
		}

	}
}

